package com.scau.service.impl;

import com.scau.dao.BrandMapper;
import com.scau.dao.GoodsMapper;
import com.scau.exception.FileUtilException;
import com.scau.pojo.Brand;
import com.scau.pojo.BrandExample;
import com.scau.pojo.Goods;
import com.scau.service.IBrandService;
import com.scau.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Siaoja
 */
@Service
public class BrandServiceImpl implements IBrandService {

    @Autowired
    BrandMapper brandMapper;

    @Value("${server.address}")
    private String serverAddress;
    @Value("${store.brand.dir}")
    private String storeDir;
    @Value("${store.brand.mapping}")
    private String storeMapping;

    @Override
    public List<Brand> getBrands(String username) {
        BrandExample brandExample = new BrandExample();
        brandExample.createCriteria().andUsernameEqualTo(username);
        List<Brand> brandList = brandMapper.selectByExample(brandExample);
        return brandList;
    }

    @Override
    public Brand getBrand(String id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    @Override
    public void addBrand(Brand brand, MultipartFile logo) throws FileUtilException {
        //生成id
        String id = UUID.randomUUID().toString().replace("-","");
        //生成图片文件路径
        String absoluteDir = generateDir(0,brand);
        String mappingDir = generateDir(1,brand);
        String fileName = generateFileName(logo, id);
        FileUtil.createDir(absoluteDir);

        brand.setId(id);
        brand.setLogoUrl(serverAddress+mappingDir+fileName);

        System.out.println("Brand:" + brand.toString());

        brandMapper.insert(brand);
        FileUtil.saveFile(logo,absoluteDir+fileName);

    }

    @Override
    public void deleteBrand(String id) throws FileUtilException {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        brandMapper.deleteByPrimaryKey(id);
        FileUtil.deleteFile(analyzeLogoUrl(brand.getLogoUrl()));
    }

    @Override
    public void updateBrand(Brand brand, MultipartFile logo) {
        //映射路径
        String logoUrl = brand.getLogoUrl();
        //绝对路径
        String path = analyzeLogoUrl(logoUrl);
        try {
            if(logo != null){//不为空时才修改图片
                //删除图片
                FileUtil.deleteFile(path);
                brand.setLogoUrl(logoUrl.substring(0,logoUrl.lastIndexOf("."))+logo.getOriginalFilename().substring(logo.getOriginalFilename().lastIndexOf(".")));
                String absolutePath = analyzeLogoUrl(brand.getLogoUrl());

                FileUtil.saveFile(logo,absolutePath);
            } else{
                brandMapper.updateByPrimaryKeySelective(brand);
            }
            brandMapper.updateByPrimaryKey(brand);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String generateFileName(MultipartFile img,String id){
        //重命名图片
        String fileName = id + img.getOriginalFilename().substring(img.getOriginalFilename().lastIndexOf("."));
        return fileName;
    }
    private String generateDir(int type, Brand brand){
        String dir = "";
        if(type == 0){
            //生成图片文件存储的真实绝对路径
            dir = storeDir + brand.getUsername()+"/";
        }else{
            //生成访问图片文件的映射路径
            dir = storeMapping + brand.getUsername()+"/";
        }
        return dir;
    }
    //解析数据库存储的图片映射路径
    private String analyzeLogoUrl(String mappingDir){
        return storeDir + mappingDir.substring(mappingDir.lastIndexOf("/BrandLogo")+11);

    }

}
